using System;
using System.Collections.Generic;
using System.Linq;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Audio;
using Microsoft.Xna.Framework.Content;
using Microsoft.Xna.Framework.GamerServices;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Input;
using Microsoft.Xna.Framework.Media;
using FeedAntGame.Control;
using FeedAntGame.Sprite.Event;
using FeedAntGame.Sprite;
using FeedAntGame.Sprite.Combo;
using FeedAntGame.Support;


namespace FeedAntGame.Component.Dialog
{
    /// <summary>
    /// This is a game component that implements IUpdateable.
    /// </summary>
    public class SaveScoreDlg : Microsoft.Xna.Framework.DrawableGameComponent
    {
        SpriteBatch spriteBatch;
        SpriteFont font;
        Texture2D bg;
        Vector2 bgPos;
        Button ok;
        EditText name;
        public SaveScoreDlg(Game game)
            : base(game)
        {
            // TODO: Construct any child components here
        }

        /// <summary>
        /// Allows the game component to perform any initialization it needs to before starting
        /// to run.  This is where it can query for any required services and load content.
        /// </summary>
        public override void Initialize()
        {
            // TODO: AddOneCandyFeed your initialization code here

            base.Initialize();
        }

        protected override void LoadContent()
        {
            spriteBatch = new SpriteBatch(Game.GraphicsDevice);
            font = Game.Content.Load<SpriteFont>(@"Fonts\Score");
            bg = Game.Content.Load<Texture2D>(@"Images\save_score_background");
            bgPos = new Vector2(Game.Window.ClientBounds.Width / 2 - bg.Width / 2, Game.Window.ClientBounds.Height / 2 - bg.Height / 2);
            name = new EditText(Game, EditText.TYPE.NAME_INPUT, new Vector2(bgPos.X + 30, bgPos.Y  + 80));
            ok = new Button(Game, Button.TYPE.OK, new Vector2(bgPos.X + bg.Width / 2 - 37,bgPos.Y + bg.Height - 50));
            ok.Sprite.Scale = 0.5f;
            (ok.Sprite as EventSprite).Click += new EventSprite.ClickButton(onOkClick);
            base.LoadContent();
        }
        /// <summary>
        /// Allows the game component to update itself.
        /// </summary>
        /// <param name="gameTime">Provides a snapshot of timing values.</param>
        public override void Update(GameTime gameTime)
        {
            // TODO: AddOneCandyFeed your update code here

            name.Sprite.Update(gameTime, Game.Window.ClientBounds);
            ok.Sprite.Update(gameTime, Game.Window.ClientBounds);            
            if ((ok.Sprite as EventSprite).IsPlaySoundHover())
            {
                ((GameFA)Game).PlayCue(ok.Sprite.collisionCueName);
            }
            
            
            base.Update(gameTime);
        }

        public override void Draw(GameTime gameTime)
        {
            //score
            string score = ((GameFA)Game).playManager.GetCurrentScore().ToString();
            spriteBatch.Begin();
            spriteBatch.Draw(bg,
                new Rectangle((int)bgPos.X, (int)bgPos.Y
                    ,bg.Width,bg.Height),
                Color.White);
            spriteBatch.DrawString(font, score, new Vector2(bgPos.X + bg.Width / 2+6 - 6 * score.Length, bgPos.Y + bg.Height / 2 + 50), Color.White);
            name.Sprite.Draw(gameTime, spriteBatch);
            ok.Sprite.Draw(gameTime, spriteBatch);
            spriteBatch.End();
            base.Draw(gameTime);
        }

        private void onShowHand()
        {
            ((GameFA)Game).Cursor.State = Cursor.STATE_TYPE.HAND;
        }
        private void onOkClick()
        {
            if ((this.name.Sprite as EditTextSprite).Value != "")
            {
                //save to database
                PlayManager playManager = ((GameFA)Game).playManager;
                Database.HighScore.CheckAndSaveHighScore((int)playManager.GetCurrentScore(), playManager.GetCurrentLevel(),
                    (this.name.Sprite as EditTextSprite).Value);
                //finish
                Finish();
            }
        }
        public void Start()
        {
            this.Enabled = true;
            this.Visible = true;
            for (int i = 0; i < Game.Components.Count; i++)
            {
                IGameComponent component = Game.Components.ElementAt(i);
                DrawableGameComponent c = (DrawableGameComponent)component;
                if ( !(c is SaveScoreDlg) && c.Visible == true && c.Enabled == true)
                {
                    c.Enabled = false;
                }
            }
        }

        private void Finish()
        {
            this.Enabled = false;
            this.Visible = false;
            for (int i = 0; i < Game.Components.Count; i++)
            {
                IGameComponent component = Game.Components.ElementAt(i);
                DrawableGameComponent c = (DrawableGameComponent)component;
                if (c is MainMenu)
                {
                    c.Enabled = true;
                    c.Visible = true;
                }
                else
                {
                    c.Enabled = false;
                    c.Visible = false;
                }
            }
            
        }
    }
}
